package org.dallasrowing.model.dao.sqlmap;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapDaoFactory {

	private static SqlMapDaoFactory instance;
	private SqlMapClient sqlMap;

	private SqlMapDaoFactory() 
	{
	}
	
	public void initialize( Properties properties ) 
	{
		try {
			String resource = "sqlMap-config.xml";
			Reader reader = Resources.getResourceAsReader (resource);
			sqlMap = SqlMapClientBuilder.buildSqlMapClient( reader, properties );
		} catch (IOException e) {
			throw new RuntimeException( "unrecoverable configuration exception", e );
		}
	}

	public static SqlMapDaoFactory instance()
	{
		if (instance == null )
			instance = new SqlMapDaoFactory();
		
		return instance;
	}
	
	public SqlMapClient getSqlMap()
	{
		return sqlMap;
	}

}
